package com.dengke.springsecurity.impl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dengke.springsecurity.dao.AccountDAO;
import com.dengke.springsecurity.entity.Account;

//Transactional for Hibernate
@Transactional
@Service
public class AccountDAOImpl implements AccountDAO{

	@Autowired
	private SessionFactory sessionFactory;
	@Override
	public Account findAccount(String userName) {
		//数据库会话，类似于连接数据库操作（后续的关闭，资源释放）
		Session session=sessionFactory.getCurrentSession();
		//条件（针对某张表）
		Criteria criteria=session.createCriteria(Account.class);
		//组织条件内容，参数化
		criteria.add(Restrictions.eq("userName", userName));
		//执行该条件查询，返回唯一的结果
		return (Account) criteria.uniqueResult();	
	}

}
